<template>
  <view class="goods-detail-container" v-if="goods_detail.goods_name">
    <!-- 轮播图区域 -->
    <swiper class="swiper-box" circular :indicator-dots="true" :autoplay="true" :interval="3000" :duration="500">
      <swiper-item class="swiper-item" v-for="(item,i) in goods_detail.pics" :key="i">
        <image @click="preview(i)" class="swiper-image" :src="item.pics_big"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息 -->
    <view class="goods-info">
      <!-- 价格 -->
      <view class="price">
        ￥{{goods_detail.goods_price}}
      </view>
      <!-- 信息 -->
      <view class="goods-info-box">
        <view class="goods-name">
          {{goods_detail.goods_name}}
        </view>
        <view class="goods-star">
          <uni-icons type="star"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="goods-yf">
        快递：免运费
      </view>
    </view>
    <!-- 渲染图文 -->
    <rich-text :nodes="goods_detail.goods_introduce"></rich-text>
    <!-- 商品导航 -->
    <view class="goods-carts">
      <uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>

</template>

<script>
  import { mapState, mapMutations, mapGetters } from 'vuex'
  export default {
    data() {
      return {
        goods_id: 0,
        goods_detail: {},
        urls: [],
        options: [{
          icon: 'shop',
          text: '店铺',

        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
            color: '#fff'
          }
        ],
      }
    },
    onLoad(options) {
      console.log(options);
      this.goods_id = options.goods_id
      this.getGoodsDetail()
    },

    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      async getGoodsDetail() {
        const { data: res } = await this.$axios.get('/goods/detail', {
          params: {
            goods_id: this.goods_id
          }
        })
        if (res.meta.status !== 200) uni.$showMsg()
        // 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ')

        this.goods_detail = res.message
      },
      preview(i) {
        uni.previewImage({
          current: i,
          indicator: 'number',
          urls: this.goods_detail.pics.map(x => x.pics_big)
        })
      },
      onClick(e) {
        if (e.content.text === '购物车') {
          // 切换到购物车页面
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(e) {
        // 1. 判断是否点击了 加入购物车 按钮
        if (e.content.text === '加入购物车') {

          // 2. 组织一个商品的信息对象
          const goods = {
            goods_id: this.goods_detail.goods_id, // 商品的Id
            goods_name: this.goods_detail.goods_name, // 商品的名称
            goods_price: this.goods_detail.goods_price, // 商品的价格
            goods_count: 1, // 商品的数量
            goods_small_logo: this.goods_detail.goods_small_logo, // 商品的图片
            goods_state: true // 商品的勾选状态
          }
          uni.$showMsg('成功加入购物车')
          // 3. 通过 this 调用映射过来的 addToCart 方法，把商品信息对象存储到购物车中
          this.addToCart(goods)

        }
      }
    },
    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['total'])
    },
    watch: {
      total: {
        handler(newVal) {
          const option = this.options.find(x => x.text === '购物车')
          if (option) {
            option.info = newVal
          }
        },
        immediate: true
      }
    }
  }
</script>

<style lang="scss">
  .goods-detail-container {
    padding-bottom: 100rpx;
  }

  .swiper-box {
    height: 750rpx;

    .swiper-item {
      .swiper-image {
        height: 100%;
        width: 100%;
      }
    }
  }

  .goods-info {
    padding: 20rpx;

    .price {
      color: #bb0000;
      font-size: 36rpx;
    }

    .goods-info-box {
      display: flex;
      font-size: 26rpx;
      margin: 10rpx 0;

      .goods-name {
        margin-right: 10rpx;
      }

      .goods-star {
        flex: 30%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 2px solid #eee;
        color: gray;
      }
    }

    .goods-yf {
      font-size: 24rpx;
      color: gray;
    }
  }

  .goods-carts {
    position: fixed;


    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>
